package fun.sweeper.unity.controller;

import fun.sweeper.base.controller.BaseController;
import fun.sweeper.base.entity.AjaxResult;
import fun.sweeper.unity.entity.HhUser;
import fun.sweeper.unity.service.HhUserService;
import fun.sweeper.unity.utils.SelectHelpUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author chenjc
 * @since 2020/11/20 9:37
 */
@Controller
public class HhUserController extends BaseController {
    private String prefix = "hh/baseInfo";

    @Autowired
    private HhUserService hhUserService;

    @GetMapping("/hh/user/user")
    public String user(ModelMap mmap) {
        HhUser hhUser = new HhUser();
//        hhUser.setStatus(ParamsDictUtil.IS_NOT_DEL);
//        hhUser.setRoleId(ParamsDictUtil.ROLE_WORKER);
        mmap.put("workerList", hhUserService.selectHhUserList(hhUser));
        return prefix + "/user_manage";
    }

    @GetMapping("/hh/user/worker")
    public String worker() {
        return prefix + "/worker_manage";
    }

    @PostMapping("/hh/user/userList")
    @ResponseBody
    public List<HhUser> userList(HhUser hhUser) {
        return hhUserService.selectHhUserList(hhUser);
    }

    @PostMapping("/hh/user/selectList")
    @ResponseBody
    public AjaxResult selectList(HhUser hhUser) {
        List<HhUser> list = hhUserService.selectHhUserList(hhUser);
        return AjaxResult.success(SelectHelpUtil.getNameUser(list));
    }

    @GetMapping("/hh/user/workerAdd")
    public String workerAdd() {
        return prefix + "/worker_add";
    }

    @PostMapping("/hh/user/workerAdd")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult workerAdd(HhUser hhUser) {
        String phoneNumber = hhUser.getPhoneNumber();
        if (StringUtils.isNotEmpty(phoneNumber)) {
            return error("新增用户【" + hhUser.getUserName() + "】手机号码已存在");
        }
//        hhUser.setLoginName(phoneNumber);
//        hhUser.setRoleIds(new Long[]{ParamsDictUtil.ROLE_WORKER});
//        hhUser.setPassword(passwordService.encryptPassword(phoneNumber, ParamsDictUtil.DEFAULT_PASSWORD, null));
//        hhUser.setCreateBy(ShiroUtils.getLoginName());
        hhUser.setRemark("水电师傅");
//        userService.insertUser(hhUser);
//        hhUser.setWorkerId(hhUser.getUserId());
        return toAjax(hhUserService.creatHhUser(hhUser));
    }

    @GetMapping("/hh/user/userAdd")
    public String userAdd(ModelMap mmap) {
        HhUser hhUser = new HhUser();
//        hhUser.setStatus(ParamsDictUtil.IS_NOT_DEL);
//        hhUser.setRoleId(ParamsDictUtil.ROLE_WORKER);
        mmap.put("workerList", hhUserService.selectHhUserList(hhUser));
        return prefix + "/user_add";
    }

    @PostMapping("/hh/user/userAdd")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult userAdd(HhUser hhUser) {
        String phoneNumber = hhUser.getPhoneNumber();
        if (StringUtils.isNotEmpty(phoneNumber)) {
            return error("新增用户【" + hhUser.getUserName() + "】手机号码已存在");
        }
        if (hhUserService.selectHhUserByName(hhUser.getUserName()) > 0) {
            return error("新增用户【" + hhUser.getUserName() + "】已存在");
        }
//        hhUser.setLoginName(StringUtils.isBlank(phoneNumber) ? String.valueOf(System.currentTimeMillis()) : phoneNumber);
//        hhUser.setRoleIds(new Long[]{ParamsDictUtil.ROLE_USER});
//        hhUser.setPassword(passwordService.encryptPassword(hhUser.getLoginName(), ParamsDictUtil.DEFAULT_PASSWORD, null));
//        hhUser.setCreateBy(ShiroUtils.getLoginName());
//        userService.insertUser(hhUser);
        //增加系统默认信息
//        if (!ParamsDictUtil.DEFAULT_WORKER.equals(hhUser.getWorkerId())) {
//            //增加选中的师傅信息
//            hhUserService.insertHhUserWorker(hhUser);
//            hhUser.setWorkerId(ParamsDictUtil.DEFAULT_WORKER);
//        }
        return toAjax(hhUserService.creatHhUser(hhUser));
    }

    @GetMapping("/hh/user/workerEdit/{userId}")
    public String workerEdit(@PathVariable("userId") Long userId, ModelMap mmap) {
//        mmap.put("user", userService.selectUserById(userId));
        return prefix + "/worker_edit";
    }

    @PostMapping("/hh/user/workerEdit")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult workerEdit(HhUser hhUser) {
        String phoneNumber = hhUser.getPhoneNumber();
        if (StringUtils.isNotEmpty(phoneNumber)) {
            return error("师傅【" + hhUser.getUserName() + "】手机号码已存在");
        }
//        hhUser.setLoginName(phoneNumber);
//        hhUser.setRoleIds(new Long[]{ParamsDictUtil.ROLE_WORKER});
//        hhUser.setUpdateBy(ShiroUtils.getLoginName());
//        return toAjax(userService.updateUser(hhUser));
        return null;
    }

    @GetMapping("/hh/user/userEdit/{userId}")
    public String userEdit(@PathVariable("userId") Long userId, ModelMap mmap) {
        HhUser hhUser = new HhUser();
//        hhUser.setStatus(ParamsDictUtil.IS_NOT_DEL);
//        hhUser.setRoleId(ParamsDictUtil.ROLE_WORKER);
        mmap.put("workerList", hhUserService.selectHhUserList(hhUser));
//        mmap.put("user", userService.selectUserById(userId));
        return prefix + "/user_edit";
    }

    @PostMapping("/hh/user/userEdit")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult userEdit(HhUser hhUser) {
        String phoneNumber = hhUser.getPhoneNumber();
        if (StringUtils.isNotEmpty(phoneNumber)) {
            return error("新增用户【" + hhUser.getUserName() + "】手机号码已存在");
        }
//        hhUser.setLoginName(phoneNumber);
//        hhUser.setRoleIds(new Long[]{hhUser.getRoleId()});
//        hhUser.setUpdateBy(ShiroUtils.getLoginName());
        hhUserService.updateHhUserWorker(hhUser);
//        return toAjax(userService.updateUser(hhUser));
        return null;
    }

    @PostMapping("/hh/user/userInfo")
    @ResponseBody
    public AjaxResult userInfo(HhUser queryParam) {
        HhUser hhUser = hhUserService.selectHhUserById(queryParam.getUserId());
        if (StringUtils.isBlank(hhUser.getPhoneNumber())) {
//            hhUser = hhUserService.selectHhUserById(queryParam.getWorkerId());
        }
        return AjaxResult.success(null, hhUser);
    }

}
